package com.ysqcould.web.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.ysqcould.activity.domain.Activity;
import com.ysqcould.activity.mapper.ActivityMapper;
import com.ysqcould.allocation.domain.WxUser;
import com.ysqcould.allocation.domain.vo.UserVo;
import com.ysqcould.allocation.mapper.WxUserMapper;
import com.ysqcould.approval.domain.Information;
import com.ysqcould.approval.domain.vo.InformationVo;
import com.ysqcould.approval.mapper.InformationMapper;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.domain.BaseEntity;
import com.ysqcould.common.core.domain.entity.JbUser;
import com.ysqcould.common.utils.DateUtils;
import com.ysqcould.content.domain.Press;
import com.ysqcould.content.mapper.PressMapper;
import com.ysqcould.members.domain.Circle;
import com.ysqcould.members.domain.vo.CircleVo;
import com.ysqcould.members.mapper.CircleMapper;
import com.ysqcould.system.mapper.SysUserMapper;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ValidationResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 获取首页记录
 * @author Administrator
 */
@RestController
@RequestMapping("/home/page")
public class HomeController extends BaseController {

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private PressMapper pressMapper;

    @Resource
    private WxUserMapper wxUserMapper;

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private CircleMapper circleMapper;

   @Resource
   private InformationMapper informationMapper;


    @GetMapping("/count")
    public AjaxResult getHomePage(){
        JSONObject json=new JSONObject();
        final Long asId = getUserId();
        Press press=new Press();
        Activity activity=new Activity();
        Circle circle=new Circle();
        Information in=new Information();
        if (asId==1){
            press.setAsId(null);
            activity.setAsId(null);
            circle.setAsId(null);
            in.setAsId(null);
        }else {
            press.setAsId(asId);
            activity.setAsId(asId);
            circle.setAsId(asId);
            in.setAsId(asId);
        }
        //获取协会动态数
        List<Press> pressList = pressMapper.selectPressList(press);
        json.set("pressList",pressList.stream().count());
        //获取活动发布数
        List<Activity> activityList = activityMapper.selectActivityList(activity);
        json.set("activityList",activityList.stream().count());
        //获取圈子发布数
        List<CircleVo> circles = circleMapper.selectCircleList(circle);
        json.set("circles",circles.stream().count());
        //获取资讯数据
        List<InformationVo> inList = informationMapper.selectInformationList(in);
        json.set("inList",inList.stream().count());
        return AjaxResult.success(json);
    }

    @GetMapping("/num")
    public AjaxResult getNum(){
       AjaxResult result=new AjaxResult();
        final Long asId = getUserId();
        JbUser user=new JbUser();
        WxUser wxUser=new WxUser();
        if (asId==1){
            user.setId(null);
            wxUser.setAsId(null);

        }else {
            user.setId(asId);
            wxUser.setAsId(asId);

        }
        //获取入驻平台数
        List<JbUser> userList = userMapper.selectUserList(user);
        result.put("userList",userList.stream().count());
        //获取关注总数
        List<WxUser> wxUserList = wxUserMapper.selectWxUserList(wxUser);
        result.put("wxUserList",wxUserList.stream().count());
        //获取总会员数
        wxUser.setUserType(1L);
        List<WxUser> list = wxUserMapper.selectWxUserList(wxUser);
        result.put("list",list.stream().count());
        //获取个人会员数
        wxUser.setVipType(1L);
        List<WxUser> pList = wxUserMapper.selectWxUserList(wxUser);
        result.put("pList",pList.stream().count());
        //获取企业会员数
        wxUser.setVipType(2L);
        List<WxUser> cList = wxUserMapper.selectWxUserList(wxUser);
        result.put("cList",cList.stream().count());
        //获取团体会员数
        wxUser.setVipType(3L);
        List<WxUser> gList = wxUserMapper.selectWxUserList(wxUser);
        result.put("gList",gList.stream().count());
        return AjaxResult.success(result);
    }

    /**
     * 获取平台每个月/每天的关注人数变化
     */
    @GetMapping("/chartInfo")
    public AjaxResult chartInfo(){
        AjaxResult result=new AjaxResult();
        WxUser user=new WxUser();
        long userId= getUserId();
        Long asId=null;
        if (userId!=1) {
            asId=userId;
        }
        user.setAsId(asId);
        //按天统计人数
        final List<UserVo> userList = wxUserMapper.selectUserByList(user);
        //按月统计人数
        final List<UserVo> userMonthList = wxUserMapper.selectUserByMonthList(user);
        result.put("userList",userList);
        result.put("userMonthList",userMonthList);
        return result;
    }
}
